<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\module\controllers;

use app\models\AddressRecorder;
use app\models\KuaidiRecorder;
use app\models\MemberRecorder;
use app\models\PaylogRecorder;
use app\models\SetRecorder;
use yii\web\Controller;

class BaseController extends Controller{
    public $enableCsrfValidation = false;
    public $user;//登录会员

    public function beforeAction($action)
    {
        $set_model = SetRecorder::find()->where('set_type=1')->one();
        $set_param = empty($set_model->content)?[]:json_decode($set_model->content,true);
        $param = \Yii::$app->params;
        $param = array_merge($set_param,$param);
        \Yii::$app->params=$param;
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }


    public function get_percent($g_num,$g_sell){
        $num = $g_num+$g_sell;
        if($num){
            $num = intval(($g_num/$num)*100);
            $percent = 100-$num;
        }else{
            $percent=100;
        }
        return $percent;
    }


    public function check_login(){
        $token = \Yii::$app->request->post('token');
        $model = MemberRecorder::find()->where('token=:token',[':token' => $token])->one();
        if(empty($model)){
            return false;
        }
        $this->user = $model;
        return true;
    }


    public function pay_log($user_id=0,$pay_type=0,$content='',$pay_num=0,$order_id=0){
        $model = new PaylogRecorder();
        $model->user_id = $user_id;
        $model->pay_type = $pay_type;
        $model->content = $content;
        $model->pay_num = $pay_num;
        $model->order_id = $order_id;
        $model->addtime = time();
        $model->save(false);
    }


    public function get_time($time){
        $end = time()-$time;
        if($end<60){
            $str=$end."秒前";
        }elseif($end<60*60){
            $str=floor($end/60)."分钟前";
        }elseif($end<60*60*24){
            $str=floor($end/(60*60))."小时前";
        }elseif($end<60*60*24*7){
            $str=floor($end/(60*60*24))."天前";
        }else{
            $str=date('Y-m-d H:i:s',$time);
        }
        return $str;
    }

    public function generateRandomDigits($length = 8)
    {
        return substr(str_shuffle(str_repeat('0123456789', $length)), 0, $length);
    }


    //获取配送时间
    public function get_send_time($address=0){
        $is_city=0;
        $time = [];
        if($address){
            $model = AddressRecorder::find()->where('id=:id',[':id' => $address])->one();
            if($model){
                $index = empty(\Yii::$app->params['city'])?0:strpos($model->address,\Yii::$app->params['city']);
                if($index){
                    $is_city=1;
                }else{
                    $k_model = KuaidiRecorder::find()->select(['k_name','k_time','k_price','k_bprice'])->asArray()->all();
                    $k_list=[];
                    $k_name=[];
                    foreach($k_model as $v){
                        $k_list[]=$v;
                        $k_name[]=$v['k_name'];
                    }
                    if(($k_index=array_search($model->province,$k_name))>=0){
                        $k = $k_list[$k_index];
                        $time['now'] = $k_list[$k_index]['k_time']."天后";
                        $time['day'] = strtotime("+ ".intval($k_list[$k_index]['k_time'])." day",time());
                        $time['day'] = date('m-d',$time['day']);
                        $time['price'] = $k_list[$k_index]['k_price'];
                        $time['b_price'] = $k_list[$k_index]['k_bprice'];
                    }else{
                        $is_city=2;//不派送
                    }
                }
            }
        }

        $mini = date('i');
        $hour = date('H');
        $day = date('w');
        $date_arr = ['日', '一', '二', '三', '四', '五', '六'];

        $star_time = \Yii::$app->params['send_start_time'];
        $end_time = \Yii::$app->params['send_end_time'];
        $p_time = \Yii::$app->params['send_time'];

        $now_time = strtotime("+ " . ($p_time * 60) . " minute", time());
        $send_time = date('H:i', $now_time);
        if ($mini > 30) {
            $time['order_time'] = ($hour + 1) . ":00";
            $time['send_time'] = $send_time;
        } else {
            $time['order_time'] = $hour . ":30";
            $time['send_time'] = $send_time;
        }
        if($is_city) {
            $time['day'] = $date_arr[$day + 1];
            $time['now'] = "明天";
            if ($hour > $star_time && $hour < $end_time) {
                $time['day'] = $date_arr[$day];
                $time['now'] = "今天";
            } else {
                $send_time = ($p_time < 1) ? ($star_time . ":" . $p_time * 60) : ($star_time + $p_time) . ":00";
                $time['send_time'] = $send_time;
            }
            $time['price'] = \Yii::$app->params['freight'];
            $time['b_price'] = \Yii::$app->params['freight_none'];
        }else{
            $time['send_time'] = "";
        }
        $time['is_city']=$is_city;
        return $time;
    }



    public function dyj($model){
        $type = \Yii::$app->params['dyj_type'];
        $dy_user = \Yii::$app->params['dy_user'];
        $dy_key = \Yii::$app->params['dy_key'];
        $dy_sn = \Yii::$app->params['dy_sn'];
        if(empty($dy_user)||empty($dy_key)||empty($dy_sn)){
            return;
        }
        $content = '<CB>商品订单</CB><BR>';
        $content .= '名称　　　 规格     单价     数量     金额<BR>';
        $content .= '-------------------------------------------<BR>';
        $goods_list = json_decode($model->order_content,true);
        foreach($goods_list as $gv) {
            foreach ($gv['list'] as $k => $v) {
                $goods_name = $v['goods_name'];
                $goods_total_price = $v['goods_price'] * $v['goods_num'];
                $attr = implode(",",$v['attr']);
                $content .= "{$goods_name}   {$attr}     {$v['goods_price']}     {$v['goods_num']}    {$goods_total_price}<BR>";
            }
        }


        $content .= '--------------------------------------------<BR>';
        $content .= "编号：{$model->order_name}<BR>";
        $order_info = empty($model->order_info)?"":$model->order_info;
        $content .= "备注：{$order_info}<BR>";
        $content .= "合计：{$model->total_price}元<BR>";
        $address = \Yii::$app->params['address'];
//        $content .= "送货地点：{$address}<BR>";
        $tel = \Yii::$app->params['tel'];
        $content .= "联系电话：{$tel}<BR>";
        $order_time = date('Y-m-d H:i:s',$model->addtime);
        $content .= "下单时间：{$order_time}<BR>";
        $content .= "<LOGO>";

        $url = "https://api.feieyun.cn/Api/Open/";
        $arr['user'] = $dy_user;
        $time = time();
        $arr['stime'] = $time;
        $arr['sig'] = sha1($dy_user.$dy_key.$time);
        $arr['apiname'] = "Open_printMsg";
        $arr['sn'] = \Yii::$app->params['dy_sn'];
        $arr['content'] = $content;
        $post_string="";
        foreach($arr as $k=>$v){
            $post_string.=urlencode($k)."=".urlencode($v)."&";
        }
        $num = \Yii::$app->params['dy_num'];
        $post_string.=' times'."={$num}";
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$post_string);
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_exec($curl);


    }
}